package extend.client.base;

import extend.client.format.Formatter;
import extend.client.util.Reflection;

import java.util.HashMap;
import java.util.Map;

/**
 * 请求配置
 *<B>说明：</B>
 *<pre>
 * 略
 *</pre>
 *<B>示例：</B>
 *<pre>
 *  略
 *</pre>
 *<B>日志：</B>
 *<pre>
 *  略
 *</pre>
 *<B>注意事项：</B>
 *<pre>
 *  略
 *</pre>
 */
public class RequestParams
{
    /**
     * url 域名部分
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     */
    protected String baseUrl;

    /**
     * 请求对应通信类
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected String transport = "extend.client.httpclient.HttpClient";

    /**
     * 请求参数的格式化方式
     *<B>说明：</B>
     *<pre>
     *  比如json
     *</pre>
     */
    protected String formatter = "extend.client.format.JsonFormatter";

    /**
     * header 信息
     *<B>说明：</B>
     *<pre>
     *  略
     *</pre>
     */
    protected Map<String,String> headers = new HashMap<>();

    /**
     * http 请求method
     *<B>说明：</B>
     *<pre>
     * 略
     *</pre>
     */
    private String method = "GET";

    protected String request = "extend.client.base.Request";

    protected String response = "extend.client.httpclient.HttpClientResponse";



    public RequestParams(Map<String,Object> options)
    {
        if (options != null) {
            Reflection.setAttrs(this,options);
        }
    }

    public void setBaseUrl(String baseUrl)
    {
        this.baseUrl = baseUrl;
    }

    public String getBaseUrl()
    {
        return this.baseUrl;
    }

    public void setTransport(String transport)
    {
        this.transport = transport;
    }

    public String getTransport()
    {
        return this.transport;
    }

    public Transport createTransport()
    {
        try {
            Class transport = Class.forName(this.transport);
            return (Transport)transport.newInstance();
        } catch (ClassNotFoundException ex) {
            return null;
        } catch (Exception ex) {
            return null;
        }
    }

    public void setFormatter(String formatter)
    {
        this.formatter = formatter;
    }

    public String getFormatter()
    {
        return this.formatter;
    }

    public void setMethod(String method)
    {
        this.method = method;
    }

    public String getMethod()
    {
        return this.method;
    }

    public String buildUrl(String url)
    {
        if (this.baseUrl != null) {
            return this.baseUrl + url;
        } else {
            return url;
        }
    }

    public Map<String,String> getHeader()
    {
        return this.headers;
    }


    public Request createRequest()
    {
        try {
            Class requestClazz = Class.forName(this.request);
            Request request = (Request)requestClazz.newInstance();
            request.setRequestParams(this);
            return request;
        } catch (ClassNotFoundException ex) {
            return null;
        } catch (Exception ex) {
            return null;
        }
    }

    public Response createResponse()
    {
        try {
            Class requestClazz = Class.forName(this.response);
            Response request = (Response)requestClazz.newInstance();
            return request;
        } catch (ClassNotFoundException ex) {
            return null;
        } catch (Exception ex) {
            return null;
        }
    }

    public Formatter createFormatter()
    {
        try {
            Class formatterClazz = Class.forName(this.formatter);
            Formatter formatter = (Formatter)formatterClazz.newInstance();
            return formatter;
        } catch (ClassNotFoundException ex) {
            return null;
        } catch (Exception ex) {
            return null;
        }
    }


}
